package com.myebills.ebpsfrontend.cust.controller;

import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
  * Override to define a servlet that requires an i-Series transaction,
  * and requires user to be logged on.
  */
public abstract class EbppxTransactionServlet extends EbppxSessionServlet {
    /**
     * Override to perform actions after transaction is committed.
     * When overriding, the convention will be to call super.doPostTransaction()
     * <b>after</b> invoking any other operations that may affect state of servlet.
     */
    protected void doPostTransaction(
        HttpServletRequest req,
        HttpServletResponse res)
    throws IOException, ServletException {}

    /**
     * Override to perform actions before transaction is begun.
     * When overriding, the convention will be to call super.doPostTransaction()
     * <b>before</b> invoking any other operations that may affect state of servlet.
     */
    protected void doPreTransaction(
        HttpServletRequest req,
        HttpServletResponse res)
    throws IOException, ServletException {}

    /**
     * Connect to the database, begin an i-Series transaction, and call doTransaction().
     * Responsible for transaction commit or rollback, and disconnecting from database.
     * Dispatches request to value returned by doTransaction().
     * Part of Template pattern (GOF).
     */
    protected final void doSession(
        HttpServletRequest req,
        HttpServletResponse res)
    throws ServletException, IOException {
        final String fName = "doSession";
        ILogger trcLogger = getTraceLogger(req);

        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_ENTRY, this, fName);

        final HttpSession session = req.getSession(false);

        if (null == session)
            throw new IllegalStateException("Cannot call perform session when session is null!");

        try
        {
            activateISeriesEngine(req, fName);
            doPreTransaction(req, res);
            beginTransaction(req, fName);
            doTransaction(req, res);
            commitTransaction(req, fName);
            doPostTransaction(req, res);
        }
        catch (ServletException e) {
            // Just re-throw these
            throw e;
        }
        catch (Throwable t)
        {
            throw new ServletException(t.toString(), t);
        }
        finally
        {
            rollbackTransaction(req, fName);
            deactivateISeriesEngine(req, fName);
        }

        if (trcLogger.isLogging())
            trcLogger.exit(IRecordType.TYPE_INFO, this, fName);
    }

    /**
     * Override to perform an i-Series operations that must occur within an i-Series transaction.
     * @return URL of JSP to forward to.
     */
    protected abstract void doTransaction(
        HttpServletRequest req,
        HttpServletResponse res)
    throws ServletException, IOException;
}